﻿//创体创建类
//Powered by Silly 2006.11
import mx.transitions.Tween.*;
import mx.utils.Delegate;
class CRIAP.Silly.MCCreator {
	private var BackgroundMC:MovieClip;
	private var TemplateName:String;
	//构造函数需要[宿主MC]及[模板名称]
	public function MCCreator(_BackgroundMC:MovieClip, _TemplateName:String) {
		if (_BackgroundMC) {
			BackgroundMC = _BackgroundMC;
		} else {
			BackgroundMC = _root;
		}
		if (_TemplateName) {
			TemplateName = _TemplateName;
		} else {
			TemplateName = "DefaultTemplate";
		}
	}
	public function ShowWindow(value:Object) {
		var _InstanceName:String = value.InstanceName;
		var _InstanceCaption:String = value.InstanceCaption;
		var _Left:Number = Math.floor(value.Left);
		var _Top:Number = Math.floor(value.Top);
		var _InitValue:Object = {
			InstanceCaption:_InstanceCaption,
			MoviePath:value.MoviePath
		};
		var iExist:Boolean = false;
		for (var iMCArray in BackgroundMC) {
			if (_InstanceName eq iMCArray) {
				iExist = true;
			}
		}
		if (iExist) {
			var tw = new mx.transitions.Tween(BackgroundMC[_InstanceName], '_alpha', mx.transitions.easing.Strong.easeOut, 0, 100, 30);
		} else {
			//trace(BackgroundMC+".attachMovie");
			BackgroundMC.attachMovie(TemplateName, _InstanceName, BackgroundMC.getNextHighestDepth(), {_x:_Left, _y:_Top, InitValue:_InitValue});
			var tw = new mx.transitions.Tween(BackgroundMC[_InstanceName], '_alpha', mx.transitions.easing.Strong.easeOut, 0, 100, 30);
		}
	}

	public function DeleteWindow(value:String) {
		for (var iMCArray in BackgroundMC) {
			if (value eq iMCArray) {
				var tw = new mx.transitions.Tween(BackgroundMC[value], '_alpha', mx.transitions.easing.Strong.easeIn, 100, 0, 10);
				//这里有一个技巧。在tw的完成函数中无法探测类中的变量值，最终导致无法析构MC，这时可以通过tw作为Object载体进行传值 
				tw.BackgroundMC = BackgroundMC;
				tw.InstanceName = value;
				tw.onMotionFinished = function() {
					var _BackgroundMC:String = tw.BackgroundMC;
					var _InstanceName:String = tw.InstanceName;
					eval(_BackgroundMC[_InstanceName]).removeMovieClip();
				};
			}
		}
	}
	public function DeleteAllWindow() {
		for (var iMCArray in BackgroundMC) {
			//BackgroundMC[iMCArray].removeMovieClip();
			DeleteWindow(iMCArray);
		}
	}
}
